//
//  LoadAPPBaseConfig.m
//  com.yjwh.yj
//
//  Created by ferver7 on 2018/10/20.
//  Copyright © 2018年 ferver7. All rights reserved.
//

#import "LoadAPPBaseConfig.h"
#import "AppAntiqueKinds.h"
#import "AppUserBaseModel.h"
#import "AppBaseConfig.h"
#import "AppExpressInfoModel.h"
#import "StudyfieldModel.h"
#import "LaunchAppADModel.h"
#import "AppShareTitleModel.h"

@implementation LoadAPPBaseConfig
+(void)loadAppAntiqueKindAPI
{
//    [PPNetworkHelper POST:GETALLCLASSFY parameters:nil success:^(id responseObject) {
//        if (YJPPSUCCESS(responseObject)) {
//            [AppAntiqueKinds clearTable];
//            dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
//            dispatch_async(q1, ^{
//                for (NSDictionary * infoDic in responseObject[@"msg"]) {
//                    AppAntiqueKinds * model = [[AppAntiqueKinds alloc]init];
//                    model.pk = [infoDic[@"id"] intValue];
//                    model.ID = infoDic[@"id"];
//                    model.name = infoDic[@"name"];
//                    [model save];
//                }
//            });
//        }else{
////            [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"desc"]];
//        }
//    } failure:^(NSError *error) {
//        
//    } showLoading:nil];
}
+(void)loadAppsStudyfieldList
{
//    [PPNetworkHelper POST:STUDYFIELDLIST parameters:nil success:^(id responseObject) {
//        if (YJPPSUCCESS(responseObject)) {
//            [StudyfieldModel clearTable];
//            dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
//            dispatch_async(q1, ^{
//                for (NSDictionary * infoDic in responseObject[@"msg"]) {
//                    StudyfieldModel * model = [[StudyfieldModel alloc]init];
//                    model.pk = [infoDic[@"id"] intValue];
//                    model.ID = infoDic[@"id"];
//                    model.name = infoDic[@"name"];
//                    [model save];
//                }
//            });
//        }else{
//            //            [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"desc"]];
//        }
//    } failure:^(NSError *error) {
//        
//    } showLoading:nil];
}
+(void)loadAppUserModedata
{
    NSDictionary * info = @{@"id":[AppUserBaseModel sharedUserData].uid};
    [PPNetworkHelper POST:2 parameters:info success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            NSDictionary * infoDic = responseObject[@"msg"];
            AppUserBaseModel * appModel = [[AppUserBaseModel alloc]init];
            [appModel setValuesForKeysWithDictionary:infoDic];
            appModel.uid = infoDic[@"id"];
            appModel.token = [AppUserBaseModel sharedUserData].token;
            [AppUserBaseModel storeUserInfoWithModel:appModel];
        }else{
//            [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"desc"]];
        }
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}
+(void)loadAppConfigInfo
{
//    [PPNetworkHelper POST:GETCONFIG parameters:nil success:^(id responseObject) {
//        if (YJPPSUCCESS(responseObject)) {
//            UserDefaultsSynchronize(responseObject[@"msg"][@"appHtmlUrl"], @"NETworkArticleDetailURL");
//            [AppBaseConfig clearTable];
//            dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
//            dispatch_async(q1, ^{
//                NSDictionary * infoDic = responseObject[@"msg"];
//                AppBaseConfig * config = [[AppBaseConfig alloc]init];
//                config.pk = 1;
//                [config setValuesForKeysWithDictionary:infoDic];
//                [config save];
//            });
//        }else{
////            [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"desc"]];
//        }
//    } failure:^(NSError *error) {
//        
//    } showLoading:nil];
}
+(void)loadAppLogisticsModedata
{
//    NSDictionary * info = @{@"pgNo":@1,@"num":@100,@"expressName":@""};
//    [PPNetworkHelper POST:AUCTIONRUSEREXPRESS parameters:info success:^(id responseObject) {
//        if (YJPPSUCCESS(responseObject)) {
//            [AppExpressInfoModel clearTable];
//            dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
//            dispatch_async(q1, ^{
//                for (NSDictionary * infoDic in responseObject[@"msg"]) {
//                    AppExpressInfoModel * model = [[AppExpressInfoModel alloc]init];
//                    model.pk = [infoDic[@"id"] intValue];
//                    model.express_name = infoDic[@"express_name"];
//                    model.express_code = infoDic[@"express_code"];
//                    [model save];
//                }
//            });
//        }else{
//            
//        }
//    } failure:^(NSError *error) {
//        
//    } showLoading:nil];
}
+(void)loadLaunchAppADInfo
{
    NSDictionary * info = @{@"osType":@"iOS"};
    [PPNetworkHelper POST:system_startpage parameters:info success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            NSDictionary * infoDic = responseObject[@"msg"];
            LaunchAppADModel * adModel = [[LaunchAppADModel alloc]init];
            adModel.ID = infoDic[@"id"];
            [adModel setValuesForKeysWithDictionary:infoDic];
            
            LaunchAppADModel * jkADMoldel = [[LaunchAppADModel findAll] firstObject];
            if (jkADMoldel.ID.integerValue == adModel.ID.integerValue ) {//同一个广告
                //拼接缓存目录
                NSString *downloadDir = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"ADDownImages%@",adModel.ID]];
                //打开文件管理器
                NSFileManager *fileManager = [NSFileManager defaultManager];
                //创建Download目录
                [fileManager createDirectoryAtPath:downloadDir withIntermediateDirectories:YES attributes:nil error:nil];
                //拼接文件路径
                NSString *filePath = [downloadDir stringByAppendingPathComponent:@"aDshow.png"];
                
                if ([fileManager fileExistsAtPath:filePath]) {
                    
                }else{//如果图片未下载成功 重新下载
                    [LoadAPPBaseConfig downImageWithADModel:adModel];
                }
//                [LoadAPPBaseConfig downImageWithADModel:adModel];
            }else{
                if (jkADMoldel) {
                     [LaunchAppADModel deleteObjects:@[jkADMoldel]];
                    //拼接缓存目录
                    NSString *downloadDir = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"ADDownImages%@",jkADMoldel.ID]];
                    //打开文件管理器
                    NSFileManager *fileManager = [NSFileManager defaultManager];
                    //创建Download目录
                    [fileManager createDirectoryAtPath:downloadDir withIntermediateDirectories:YES attributes:nil error:nil];
                    //拼接文件路径
                    NSString *filePath = [downloadDir stringByAppendingPathComponent:@"aDshow.png"];
                    
                    if ([fileManager fileExistsAtPath:filePath]) {//同时把下载的图片删除
                        [fileManager isDeletableFileAtPath:downloadDir];
                    }else{
                        
                    }
                }
                dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
                dispatch_async(q1, ^{
                    [adModel save];//保存新广告同时下载图片
                    [LoadAPPBaseConfig downImageWithADModel:adModel];
                });
            }
//            [AppExpressInfoModel clearTable];
            
        }else{
            
        }
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}

+(void)downImageWithADModel:(LaunchAppADModel *)adModel
{
    [PPNetworkHelper downloadWithURL:adModel.picUrl fileDir:[NSString stringWithFormat:@"ADDownImages%@",adModel.ID] progress:^(NSProgress *progress) {
        
    } success:^(NSString *filePath) {
        
    } failure:^(NSError *error) {
        
    }];
}
+(void)loadSystemGetShareTitle
{
    [PPNetworkHelper POST:system_getShareTitle parameters:nil success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            [AppShareTitleModel clearTable];
            dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
            dispatch_async(q1, ^{
                for (NSDictionary * infoDic in responseObject[@"msg"][@"list"]) {
                    AppShareTitleModel * model = [[AppShareTitleModel alloc]init];
                    model.pk = [infoDic[@"id"] intValue];
                    model.ID = infoDic[@"id"];
                    model.status = infoDic[@"status"];
                    model.text = infoDic[@"text"];
                    [model save];
                }
            });
        }else{
            //            [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"desc"]];
        }
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}
@end
